<template>
  <div class="addressAdd">
    <!-- 引入公共头部 -->
    <NavBar title="地址添加" @click="toNext" back/>
    <van-address-edit
      :area-list="areaList"
      :address-info="addressInfo"
      show-set-default
      @save="onSave"
    />
  </div>
</template>
<script>
import Vue from 'vue'
import { AddressEdit, Toast } from 'vant'
import NavBar from '../../components/common/NavBar.vue'
import userApi from '../../api/user'
import areaData from '@/assets/json/area.json'
Vue.use(AddressEdit)
export default {
  name: 'addressAdd',
  data () {
    return {
      areaList: areaData.re,
      addressInfo: {id: 0, name: '', tel: '', province: '', city: '', county: '', addressDetail: '', isDefault: false, areaCode: ''}
    }
  },
  mounted () {
    this.getConfirmNum()
  },
  methods: {
    toNext: function () {
      if (this.$route.query.confirmNum) {
        this.$router.go(-1)
      } else {
        this.$router.push('/user/address')
      }
    },
    getConfirmNum: function () {
      if (this.$route.query.confirmNum) {
        this.addressInfo.isDefault = true
      }
    },
    onSave: function (val) {
      const _this = this
      userApi.addAddress(val).then(function (res) {
        if (res.data.success) {
          if (_this.$route.query.confirmNum) {
            _this.$router.go(-1)
          } else {
            Toast.success('保存成功！')
            _this.$router.push('/user/address')
          }
        } else {
          Toast.fail(res.data.msg)
        }
      }).catch(function () {
      })
    }
  },
  components: {
    NavBar
  }
}
</script>
